import {getActivityDetail, getTaskList} from "../../api/activity";

import Util from '../../utils/util.js';

const app = getApp();
Page({
  data: {
    parameter: {
      isBack: true,
    },
    opacity: 0,

    tabs: [
      {title: "进行中", amount: 0, value: 1, id : "doing"},
      {title: "已完成", amount: 0, value: 2, id : "done"},
      {title: "当前积分", amount: 0, value: 3, id : "total_integral"},
    ],

    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    current_index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    this.setData({
      bottom: 30,
      activity_id: options.id || 0,
      navH: app.globalData.navHeight
    })

    if (Util.isEmpty(this.data.activity_id)) {
      if (getCurrentPages().length > 1) {
        Util.Tips({title: "数据错误"})
      } else {
        Util.Tips({title: "数据错误"}, {tab: 1, url: "/pages/task/index"})
      }
      return;
    }

    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo || null
      })

      wx.showLoading({title: '正在加载...'});
      getActivityDetail(that.data.activity_id).then(res => {
        that.setData({...res.data});
      }).catch(errmsg => {
        Util.Tips({title: errmsg}, {tab: 3})
      }).finally(() => {
        wx.hideLoading();
      })
    }).then(() => {
      that.onLoadTabbar();
      that.getLists();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }
    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['activity_id'] = that.data.activity_id || 0;

    that.setData({
      loading: true,
      loadTitle: '正在加载...'
    });
    let lists = that.data.lists || []
    getTaskList(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;
      lists = Util.SplitArray(list, lists);
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      wx.stopPullDownRefresh()
    })
  },


  changeNav(e) {
    this.setData({
      current_index: e.currentTarget.dataset.v,
      loadend: false,
      loading: false,
      page: 1,
      lists: []
    });

    this.getLists();
  },

  previewImage: function (e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loading: false,
      loadend: false,
      loadTitle: '加载更多内容',
      lists: [],
      page: 1,
    })
    this.getLists();
  },

  goBack(e) {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: '/pages/activity/index'
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  onLoadTabbar: function (e) {//底部tabbar返回数据

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})